package com.openlogic.common.logic.utils;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.openlogic.api.logic.dto.RuleFuncLibDto;
import com.openlogic.api.logic.dto.RuleFuncMethodDto;
import com.openlogic.common.logic.func.enums.FuncLibTypeEnums;

/**
* @ClassName: ResExprUtil
* @Description: XML解析工具
* @author weiyongpeng
* @date 2024年7月11日 下午2:38:11
 */
public class FuncXmlParseUtils {

	/**
	* 描述：获取函数库列表JSON数据
	* @Title: resCustomExperConfig
	* @param path
	* @return
	* @author weiyongpeng
	* @date  2024年7月22日 下午5:13:01
	 */
	public static JSONObject resCustomExperConfig(String path) {
		try {
			SAXReader reader = new SAXReader();
			Document document = reader.read(new File(path));
			Element node = document.getRootElement();
			return nodeToJSONObject(node);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	* 描述：获取函数库列表DTO集合
	* @Title: getFuncExperConfig
	* @param path
	* @return
	* @author weiyongpeng
	* @date  2024年7月22日 下午5:13:23
	 */
	public static List<RuleFuncLibDto> getFuncExperConfig(String path) {
		try {
			SAXReader reader = new SAXReader();
			Document document = reader.read(new File(path));
			Element node = document.getRootElement();
			return nodeToFuncDto(node);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return new ArrayList<RuleFuncLibDto>();
	}
	
	/**
	* 描述：将规则函数库xml转化为前端所需集合对象
	* @Title: getFuncLibList
	* @param path
	* @return
	* @author weiyongpeng
	* @date  2024年7月23日 上午9:45:20
	 */
	public static List<Map<String, RuleFuncLibDto>> getFuncLibList(String path) {
		List<Map<String, RuleFuncLibDto>> result = new ArrayList<>();
		try {
			List<RuleFuncLibDto> ruleFunList = getFuncExperConfig(path);
			for (RuleFuncLibDto ruleFuncLibDto : ruleFunList) {
				String funcLibType = ruleFuncLibDto.getFuncLibType();
				if(funcLibType.equals(FuncLibTypeEnums.MATH.getFuncTypeCode())) {
					Map<String,RuleFuncLibDto> funMap = new HashMap<>();
					funMap.put(FuncLibTypeEnums.MATH.getFuncDescByCode(), ruleFuncLibDto);
					result.add(funMap);
				}
				if(funcLibType.equals(FuncLibTypeEnums.CUS_MATH.getFuncTypeCode())) {
					Map<String,RuleFuncLibDto> funMap = new HashMap<>();
					funMap.put(FuncLibTypeEnums.CUS_MATH.getFuncDescByCode(), ruleFuncLibDto);
					result.add(funMap);
				}
				if(funcLibType.equals(FuncLibTypeEnums.CUS_STRING.getFuncTypeCode())) {
					Map<String,RuleFuncLibDto> funMap = new HashMap<>();
					funMap.put(FuncLibTypeEnums.CUS_STRING.getFuncDescByCode(), ruleFuncLibDto);
					result.add(funMap);
				}
				if(funcLibType.equals(FuncLibTypeEnums.DATE.getFuncTypeCode())) {
					Map<String,RuleFuncLibDto> funMap = new HashMap<>();
					funMap.put(FuncLibTypeEnums.DATE.getFuncDescByCode(), ruleFuncLibDto);
					result.add(funMap);
				}
				if(funcLibType.equals(FuncLibTypeEnums.CUS_COLLECTION.getFuncTypeCode())) {
					Map<String,RuleFuncLibDto> funMap = new HashMap<>();
					funMap.put(FuncLibTypeEnums.CUS_COLLECTION.getFuncDescByCode(), ruleFuncLibDto);
					result.add(funMap);
				}
				if(funcLibType.equals(FuncLibTypeEnums.CUS_STATIS.getFuncTypeCode())) {
					Map<String,RuleFuncLibDto> funMap = new HashMap<>();
					funMap.put(FuncLibTypeEnums.CUS_STATIS.getFuncDescByCode(), ruleFuncLibDto);
					result.add(funMap);
				}
			}
			return result;
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		return new ArrayList<>();
	}
	
	/**
	* 描述：xml解析为DTO
	* @Title: nodeToFuncDto
	* @param node
	* @return
	* @author weiyongpeng
	* @date  2024年7月22日 下午5:13:44
	 */
	public static List<RuleFuncLibDto> nodeToFuncDto(Element node) {
		List<RuleFuncLibDto> listFuncLibs = new ArrayList<>();

		List<Element> listElement = node.elements();
		if (!listElement.isEmpty()) {
			for (Element e : listElement) {
				RuleFuncLibDto ruleFuncLibDto = new RuleFuncLibDto();
				List<Map<String, RuleFuncMethodDto>> listMethods = new ArrayList<>();
				
				JSONObject r1 = new JSONObject();
				List<Attribute> listAttr = e.attributes();
				for (Attribute attr : listAttr) {
					String key = attr.getName();
					String value = attr.getValue();
					if(key.equals("name")) {
						ruleFuncLibDto.setFuncLibClass(value);
					}
					if(key.equals("desc")) {
						ruleFuncLibDto.setFuncLibDesc(value);
					}
					if(key.equals("type")) {
						ruleFuncLibDto.setFuncLibType(value);
					}
					r1.put(key, value);
				}

				List<Element> liste = e.elements();
				for (Element e2 : liste) {
					RuleFuncMethodDto ruleFuncMethodDto = new RuleFuncMethodDto();
					Map<String, RuleFuncMethodDto> map = new HashMap<>();
					JSONObject r2 = new JSONObject();
					// 获取method的属性
					List<Attribute> list2 = e2.attributes();
					for (Attribute methodAttr : list2) {
						String key = methodAttr.getName();
						String value = methodAttr.getValue();
						if(key.equals("name")) {
							ruleFuncMethodDto.setName(value);
						}
						if(key.equals("return")) {
							ruleFuncMethodDto.setReturn_(value);
						}
						if(key.equals("multiple")) {
							ruleFuncMethodDto.setMultiple(value);
						}
						if(key.equals("desc")) {
							value.replaceAll("\"", "&quot;");
							ruleFuncMethodDto.setDesc(value);
						}
						if(key.equals("title")) {
							ruleFuncMethodDto.setTitle(value);
						}
						r2.put(key, value);
					}
					// 获取Params
					List<Element> listp = e2.elements();
					if (!listp.isEmpty()) {
						Iterator<Element> localIterator6;
						List<Map<String, Object>> methodParmsList = new ArrayList<>();
						for (Iterator<Element> localIterator5 = listp.iterator(); localIterator5.hasNext(); localIterator6
								.hasNext()) {
							Element ep = localIterator5.next();
							List<Element> methodsList = ep.elements();
							// 方法参数列表
							for (Element eleP : methodsList) {
								// 获取param的属性
								List<Attribute> attributes = eleP.attributes();
								JSONObject rp = new JSONObject();
								Map<String, Object> methodParams = new HashMap<>();
								for (Attribute attribute : attributes) {
									rp.put(attribute.getName(), attribute.getValue());
									methodParams.put(attribute.getName(), attribute.getValue());
								}
								methodParmsList.add(methodParams);
							}
							localIterator6 = methodsList.iterator();
							continue;
						}
						ruleFuncMethodDto.setParams(methodParmsList);
					}

					map.put(ruleFuncMethodDto.getName(), ruleFuncMethodDto);
					listMethods.add(map);
				}
				ruleFuncLibDto.setFuncLibMethods(listMethods);
				listFuncLibs.add(ruleFuncLibDto);
			}
		}
		return listFuncLibs;
	}

	/**
	* 描述：xml解析为JSON
	* @Title: nodeToJSONObject
	* @param node
	* @return
	* @author weiyongpeng
	* @date  2024年7月22日 下午5:16:13
	 */
	public static JSONObject nodeToJSONObject(Element node) {
		JSONObject result = new JSONObject();

		List<Element> listElement = node.elements();
		if (!listElement.isEmpty()) {
			for (Element e : listElement) {
				JSONObject r1 = new JSONObject();
				List<Attribute> listAttr = e.attributes();
				for (Attribute attr : listAttr) {
					r1.put(attr.getName(), attr.getValue());
				}
				result.put(e.attributeValue("name"), r1);

				List<Element> liste = e.elements();
				JSONObject r3 = new JSONObject();
				for (Element e2 : liste) {
					JSONObject r2 = new JSONObject();
					// 获取method的属性
					List<Attribute> list2 = e2.attributes();
					for (Attribute attr2 : list2) {
						r2.put(attr2.getName(), attr2.getValue());
					}

					// 获取Params
					List<Element> listp = e2.elements();
					if (!listp.isEmpty()) {
						Iterator<Element> localIterator6;
						for (Iterator<Element> localIterator5 = listp.iterator(); localIterator5.hasNext(); localIterator6
								.hasNext()) {
							Element ep = localIterator5.next();
							List<Element> listp2 = ep.elements();
							List<Object> list = new ArrayList<>();
							for (Element eleP : listp2) {
								// 获取param的属性
								List<Attribute> attributes = eleP.attributes();
								JSONObject rp = new JSONObject();
								for (Attribute attribute : attributes) {
									rp.put(attribute.getName(), attribute.getValue());
								}
								list.add(rp);
							}
							r2.put("param", new JSONArray(list));
							localIterator6 = listp2.iterator();
							continue;
						}

					}

					r3.put(e2.attributeValue("name"), r2);
				}

				r1.put("method", r3);
			}
		}
		return result;
	}
}